/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-app-api
*
* @File name : AppSosService.java
*
* @Author : Administrator
*
* @Date : 2017年11月29日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年11月29日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.marketing.app.api.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.Response;
import org.marketing.common.constants.ExceptionMessage;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.marketing.app.api.model.TtSoshelp;

/**
 * @author Administrator 
 * @date 2017年11月29日
 */

@Service("appSosService")
public class AppSosService {

    private Logger logger = Logger.getLogger(AppSosService.class);
    
    @Autowired
    private RoadSosServiceInterface roadSosServiceInterface;
    

    public Integer roadsideAssistance(TtSoshelp ttSoshelp,String userId){
        if(CheckUtil.NullOrEmpty(userId)){
            throw new ServiceBizException(ExceptionMessage.USERID_IS_NULL);
        }
        Response<Map<String, Object>> result = roadSosServiceInterface.roadsideAssistance(ttSoshelp, userId);
        logger.info("返回信息为："+result);
        if(CheckUtil.NullOrEmpty(result)){
            throw new ServiceBizException(ExceptionMessage.APP_SEND_SOS_FAIL);
        }
        Integer resultCode = result.getResultCode();
        return resultCode;
    }
    
    @SuppressWarnings("unchecked")
    public List<Map<String,Object>> getDealer(TtSoshelp ttSoshelp,String userId){
        List<Map<String,Object>> list = new ArrayList<>();
        if(CheckUtil.NullOrEmpty(userId)){
            throw new ServiceBizException(ExceptionMessage.USERID_IS_NULL);
        }
        Response<Map<String, Object>> result = roadSosServiceInterface.getDealer(ttSoshelp, userId);
        logger.info("返回信息为："+result);
        if(CheckUtil.NullOrEmpty(result)){
            throw new ServiceBizException(ExceptionMessage.APP_SOS_DEALER_ERROR);
        }
        Integer resultCode = result.getResultCode();
        if(resultCode==0){
            throw new ServiceBizException(ExceptionMessage.APP_SOS_DEALER_ERROR);
        }
        Map<String, Object> resultMap = new HashMap<String,Object>();
        Map<String, Object> first = new HashMap<String,Object>();
        Map<String, Object> second = new HashMap<String,Object>();
        Map<String, Object> third = new HashMap<String,Object>();
        resultMap = result.getData();
        if(!CheckUtil.NullOrEmpty(resultMap.get("First"))){
            first = (Map<String, Object>) resultMap.get("First");
            first.put("flag", "最近店");
            list.add(first);
        }
        if(!CheckUtil.NullOrEmpty(resultMap.get("Second"))){
            second = (Map<String, Object>) resultMap.get("Second");
            second.put("flag", "次近店");
            list.add(second);
        }
        if(!CheckUtil.NullOrEmpty(resultMap.get("Third"))){
            third = (Map<String, Object>) resultMap.get("Third");
            third.put("flag", "");
            list.add(third);
        }
        if(CheckUtil.NullOrEmpty(resultMap)){
            throw new ServiceBizException(ExceptionMessage.APP_SOS_DEALER_NULL);
        }
        return list;
    }
}
